<template>
  <div class="category-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>分类列表</span>
          <el-button type="primary" @click="getDataList">刷新</el-button>
        </div>
      </template>

      <el-table :data="categoryList" v-loading="loading" style="width: 100%" border stripe>
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="分类名称" />
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination class="pagination" v-model:current-page="currentPage" v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { getCategoryList } from '@/api/category'

const categoryList = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const getDataList = async () => {
  loading.value = true
  const params = {
    page: currentPage.value,
    size: pageSize.value
  }
  const res = await getCategoryList(params)
  loading.value = false
  categoryList.value = res.data.list
  total.value = res.total
}

const handleSizeChange = (val) => {
  pageSize.value = val
  getDataList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getDataList()
}

onMounted(() => {
  getDataList()
})
</script>

<style scoped>
.category-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>